<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="!isCollapse ? '200px' : '64px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          router
          unique-opened
          background-color="#333744"
          text-color="#fff"
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activePath"
          active-text-color="#409EFF">
          <!-- 一级子菜单  -->
          <el-submenu
            v-for="item in menuList"
            :key="item.id"
            :index="item.id + ''">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 子菜单的内容  -->
            <el-menu-item
              v-for="sub in item.children"
              :key="sub.id"
              :index="'/' + sub.path"
              @click="saveNavState('/' + sub.path)"
              >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{sub.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- home路由子路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    name: 'Home',
    data() {
      return {
        menuList: [], // 菜单数组
        iconObj: { // 菜单图表class
          125: 'iconfont icon-user',
          103: 'iconfont icon-tijikongjian',
          101: 'iconfont icon-shangpin',
          102: 'iconfont icon-danju',
          145: 'iconfont icon-baobiao'
        },
        isCollapse: false, // 菜单是否折叠
        activePath: '' // 被激活的路由地址
      }
    },
    created () {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      logout () {
        window.sessionStorage.clear()
        this.$router.push('/login')
      },
      // 获取菜单
      async getMenuList() {
        const { data: res } = await this.$http.get('menus')
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.menuList = res.data
      },
      // 切换菜单展开，折叠
      toggleCollapse() {
        this.isCollapse = !this.isCollapse
      },
      // 保存激活的路由地址
      saveNavState(activePath) {
        this.activePath = activePath
        window.sessionStorage.setItem('activePath', activePath)
      }
    }
  }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 20px;
    color: #fff;
    > div {
      img {
        height: 60px;
      }
      display: flex;
      align-items: center;
    }
  }
  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
  .iconfont {
    margin-right: 5px;
  }
  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
